﻿using M_ApiResult;
using M_Ingredient.API.Application.Command.IngredientCommand;
using M_Ingredient.Domain;
using M_Ingredient.Domain.Dto;
using M_Quality.Domain;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace M_Ingredient.API.Controllers
{
    /// <summary>
    ///原料控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class IngredientController : ControllerBase
    {
        protected readonly IMediator mediator;

        public IngredientController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 编辑Bom里原料
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<List<Ingredient>>> BackfillBom(IngredientBackDanYuanCommand command)
        {
            ApiResult<List<Ingredient>> result = new ApiResult<List<Ingredient>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
        /// <summary>
        /// 添加原料
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddIngredient(IngredientCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {

                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
        /// <summary>
        /// 单文件上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
     
        public async Task<IActionResult> Upfile(IFormFile file)
        {
            string path = Directory.GetCurrentDirectory() + @"/wwwroot/Pic/" + file.FileName;
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            return Ok(new { code = 0, msg = "文件上传成功", data = file.FileName });
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<Ingredient>>> Show([FromQuery] IngredientShowCommand command)
        {
            ApiResult<PageDto<Ingredient>> result = new ApiResult<PageDto<Ingredient>>();
            try
            {

                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelIngredient(IngredientDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Ingredient>> BackfillIngredient([FromQuery] IngredientBackfillCommand command)
        {
            ApiResult<Ingredient> result = new ApiResult<Ingredient>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateIngredient(IngredientUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {

                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
        /// <summary>
        /// 修改质检方案状态
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateIngredientState([FromQuery] IngredientUpdateStateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "修改质检方案状态失败";
                throw;
            }
            return result;
        }

    }
}
